package com.jinlia.show.modules.system.service;

import com.jinlia.show.modules.base.constant.DeliveryStatusEnum;
import com.jinlia.show.modules.base.constant.OrderStatusEnum;
import com.jinlia.show.modules.base.model.po.DeliveryOrderPO;
import com.jinlia.show.modules.base.service.WxMessageService;
import com.jinlia.show.modules.base.system.entity.DeliveryOrder;
import com.jinlia.show.modules.base.system.entity.Order;
import com.jinlia.show.modules.base.system.service.DeliveryOrderMapperService;
import com.jinlia.show.modules.base.system.service.OrderMapperService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class DeliveryOrderService extends DeliveryOrderMapperService {
    @Value("${wxapp.version}")
    public String wxappVersion;
    @Autowired
    private OrderMapperService orderMapperService;

    @Autowired
    WxMessageService wxMessageService;
    @Autowired
    private RestTemplate wxRestTemplate;
    @Autowired
    ScheduledExecutorService scheduledExecutorService;

    @Override
    @Transactional
    public DeliveryOrder confirmDeliveryOrder(DeliveryOrder deliveryOrder) {
        DeliveryOrder confirmDeliveryOrder = super.confirmDeliveryOrder(deliveryOrder);
        //微信消息推送
        scheduledExecutorService.schedule(() -> {
            log.info(String.format("schedule当前线程=%S,延迟=%S", Thread.currentThread().getName(), "不延迟"));
            try {
                String body = wxMessageService.sendConfirmOrderMsg(deliveryOrder.getId(), wxappVersion);
                log.info("schedule当前线程={},消息模版推送结果={}", Thread.currentThread().getName(), body);
            } catch (Exception e) {
                log.error("chedule当前线程=%S,消息模版推送结果::", e);
            }
        }, 0, TimeUnit.SECONDS);
        return confirmDeliveryOrder;
    }

    public Boolean startDeliverOrder(DeliveryOrder deliveryOrder) {
        Boolean aBoolean = super.startDeliverOrder(deliveryOrder);
        //微信消息推送
        scheduledExecutorService.schedule(() -> {
            log.info(String.format("schedule当前线程=%S,延迟=%S", Thread.currentThread().getName(), "不延迟"));
            try {
                String body = wxMessageService.sendOrderDeliverMsg(deliveryOrder.getId(), wxappVersion);
                log.info("schedule当前线程={},消息模版推送结果={}", Thread.currentThread().getName(), body);
            } catch (Exception e) {
                log.error("chedule当前线程=%S,消息模版推送结果::", e);
            }
        }, 0, TimeUnit.SECONDS);
        return aBoolean;
    }

    public Boolean confirmDelivery(DeliveryOrder deliveryOrder) {
        DeliveryOrder byIdDeliveryOrder = super.getById(deliveryOrder.getId());
        this.lambdaUpdate()
                .set(DeliveryOrder::getDeliveryStatus, DeliveryStatusEnum.B.getValue())
                .set(DeliveryOrder::getCurrentErrandStatus, 2)
                .set(DeliveryOrder::getCurrentErrandType, deliveryOrder.getCurrentErrandType())
                .eq(DeliveryOrder::getId, deliveryOrder.getId())
                .update();
        Order byId = orderMapperService.getById(byIdDeliveryOrder.getOrderId());
        orderMapperService.lambdaUpdate()
                .set(Order::getOrderStatus, OrderStatusEnum.STEP_3.getValue())
                .eq(Order::getId, byId.getId())
                .update();
        return Boolean.TRUE;
    }
}
